package fileexplorer.view;

import fileexplorer.Configuration;
import fileexplorer.model.FileManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: rejeev
 * Date: 4/27/13
 * Time: 12:23 PM
 * To change this template use Leaf | Settings | Leaf Templates.
 */
public class MainWindow extends JFrame {
    private static final Logger LOGGER = LoggerFactory.getLogger(MainWindow.class);
    private FileManager fileManager = new FileManager();
    private DefaultMutableTreeNode topNode;
    private FileTree tree;
    private FileTable fileTable;
    private ExplorerMenuBar menuBar;
    public static final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

    public MainWindow(){
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(screenSize.width, screenSize.height);
        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
        initialize();
        this.setVisible(true);
        postInitialization();
    }

    private void initialize(){
        topNode = FileTree.getTree(fileManager.getDirectory(Configuration.getDefaultLocation(), Configuration.level));
        String[] columnNames = {"Name", "Size", "Modified Time"};
        Object[][] data = {{"abc.txt","1000", new Date()},
                {"xyz.txt", "5455", new Date()}};
        fileTable = new FileTable(data, columnNames, this, fileManager);
        tree = new FileTree(this, fileTable, topNode);
        Container contentPane = this.getContentPane();
        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS));
        JScrollPane treeScrollPane = new JScrollPane(tree);
        treeScrollPane.setBorder(new EmptyBorder(10,10,10,10));
        JScrollPane tableScrollPane = new JScrollPane(fileTable);
        tableScrollPane.setBorder(new EmptyBorder(10, 10, 10, 10));
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, treeScrollPane, tableScrollPane);
        splitPane.setResizeWeight(0.5);
        splitPane.setDividerLocation(screenSize.width/2);
        contentPane.add(splitPane);
        menuBar = new ExplorerMenuBar();
        this.setJMenuBar(menuBar);
        setKeyBindings();
    }

    private void postInitialization(){
        tree.postInitialize();
    }

    private void setKeyBindings(){
        JRootPane jRootPane = this.getRootPane();
        Action treeRoot = new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                DefaultMutableTreeNode root = (DefaultMutableTreeNode)tree.getModel().getRoot();
                tree.requestFocus();
                tree.setSelectionPath(new TreePath(root.getPath()));
            }
        };
        Action tableTop = new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                fileTable.requestFocus();
                fileTable.changeSelection(0, 0, false, false);
            }
        };
        jRootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.SHIFT_MASK | ActionEvent.ALT_MASK), "tree_root");
        jRootPane.getActionMap().put("tree_root", treeRoot);
        jRootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_R, ActionEvent.SHIFT_MASK | ActionEvent.ALT_MASK), "table_top");
        jRootPane.getActionMap().put("table_top", tableTop);
    }
}
